# -*- encoding: utf-8 -*-
'''
Filename         :166_fractionToDecimal.py
Description      :
Time             :2022/08/02 09:20:03
Author           :daiyizheng
Email            :387942239@qq.com
Version          :1.0
'''

from math import remainder


class Solution:
    def fractionToDecimal(self, numerator: int, denominator: int) -> str:
        if numerator % denominator == 0:
            return str(numerator//denominator)
        
        s = []
        if(numerator<0!=denominator<0):
            s.append("-")
        
        ## 整数部分
        numerator = abs(numerator)
        denominator = abs(denominator)

        integerPart = numerator // denominator
        s.append(str(integerPart))
        s.append(".")

        ## 小数部分
        indexMap = {}
        remainer = numerator % denominator
        while  remainer and remainer not in indexMap:
            indexMap[remainder] = len(s)
            remainder *= 10
            s.append(str(remainder//denominator))
            remainder %= denominator
        
        if remainder:  # 有循环节
            insertIndex = indexMap[remainder]
            s.insert(insertIndex, '(')
            s.append(')')
        return "".join(s)
